﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace GesWork.actividades
{
    public partial class actividadPupPop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["id"];
            //btnModificar.Visible = true;
            //btnEliminar.Visible = true;

            if (id == null)
            {
                //Pagina para insertar Activiadd
                //FormView1.ChangeMode(FormViewMode.Insert);
                //btnModificar.Visible = false;
                //btnEliminar.Visible = false;
            }

            else
            {
                try
                {
                    if (!Page.IsPostBack)
                    {
                        Label toUserC = (Label)FormView1.Row.FindControl("IDToUserLabelContact");
                        Label toUserE = (Label)FormView1.Row.FindControl("IDToUserLabelEnterprise");
                        Label toUserO = (Label)FormView1.Row.FindControl("IDToUserLabelOportunity");
                        Label type = (Label)FormView1.Row.FindControl("OwnerByTypeLabel");


                        if (type.Text == "0")
                            toUserO.Visible = true;
                        else if (type.Text == "1")
                            toUserE.Visible = true;
                        else if (type.Text == "2")
                            toUserC.Visible = true;


                        Session["idparent"] = null;
                    }

                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }


            }

            //try
            //{ 
            //    //Obtenemos y asignamos el usuario
            //    Label txtPropietario = (Label)FormView1.Row.FindControl("OwnerLabel");
            //    txtPropietario.Text = User.Identity.Name;
            //    dsActividades.InsertParameters["Owner"].DefaultValue = User.Identity.Name;
            //}
            //catch (Exception ex)
            //{
            //    Response.Write(ex.Message);
            //}



        }

        protected void TipoRelacion(object sender, EventArgs e)
        {
            try
            {
                DropDownList cmbType = (DropDownList)FormView1.Row.FindControl("cmbOwnerByType");
                DropDownList cmbToUser = (DropDownList)FormView1.Row.FindControl("cmbIDToUser");

                if (cmbType.SelectedValue == "0")
                {
                    cmbToUser.DataSourceID = "dsOportunity";
                }
                else if (cmbType.SelectedValue == "1")
                {
                    cmbToUser.DataSourceID = "dsEnterprise";
                }
                else if (cmbType.SelectedValue == "2")
                {
                    cmbToUser.DataSourceID = "dsContact";
                }

                cmbToUser.DataBind();


            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }


        }

        protected void TipoRelacionInsert(object sender, EventArgs e)
        {
            try
            {
                DropDownList cmbType = (DropDownList)FormView1.Row.FindControl("cmbOwnerByTypeIns");
                DropDownList cmbToUser = (DropDownList)FormView1.Row.FindControl("CmbIDToUserIns");

                if (cmbType.SelectedValue == "0")
                {
                    cmbToUser.DataSourceID = "dsOportunityIns";
                }
                else if (cmbType.SelectedValue == "1")
                {
                    cmbToUser.DataSourceID = "dsEnterpriseIns";
                }
                else if (cmbType.SelectedValue == "2")
                {
                    cmbToUser.DataSourceID = "dsContactIns";
                }

                Response.Write("<br>OWNER: " + dsActividades.InsertParameters["owner"].DefaultValue);
                Response.Write("<br>OwnerByType: " + dsActividades.InsertParameters["OwnerByType"].DefaultValue);
                Response.Write("<br>IDToUser: " + dsActividades.InsertParameters["IDToUser"].DefaultValue);
                Response.Write("<br>Subject: " + dsActividades.InsertParameters["Subject"].DefaultValue);
                Response.Write("<br>MSG_Text: " + dsActividades.InsertParameters["MSG_Text"].DefaultValue);
                Response.Write("<br>StartDate: " + dsActividades.InsertParameters["StartDate"].DefaultValue);


                cmbToUser.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }


        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            try
            {
                string id = Request.QueryString["id"];
                dsActividades.DeleteParameters["original_ID"].DefaultValue = id;
                Response.Redirect("editar.aspx?id=" + id);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                string id = Request.QueryString["id"];
                dsActividades.DeleteParameters["original_ID"].DefaultValue = id;
                dsActividades.Delete();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            Response.Redirect("activas.aspx");
        }


        protected void mostrarActividadPadre_Click(object sender, EventArgs e)
        {
            string idparent = (String)Session["idparent"];

            if (idparent == null)
            {

                idparent = ((LinkButton)FormView1.FindControl("ACC_IDParentActivityLabel")).Text;
                Session["idparent"] = idparent;
                SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["gesworkConnectionString"].ConnectionString);



                SqlDataAdapter daCliente = new SqlDataAdapter("SELECT [ID],  CASE  WHEN [StartDate] is NULL THEN '' ELSE '('+convert(varchar(20),[StartDate],103)+') '  end + [Subject] AS Subject FROM [Activity] WHERE id=" + idparent + "  ORDER by [StartDate] DESC", cn);

                DataSet ds = new DataSet();


                daCliente.Fill(ds, "Activity");
                //carga de datos de clientes en el ComboBox
                foreach (DataRow pRow in ds.Tables["Activity"].Rows)
                {


                    ((LinkButton)FormView1.FindControl("ACC_IDParentActivityLabel")).Text = pRow["subject"].ToString();

                }

                //liberar recursos
                daCliente.Dispose();
                ds.Dispose();

            }
            else
            {
                ((LinkButton)FormView1.FindControl("ACC_IDParentActivityLabel")).Text = idparent;
                Session["idparent"] = null;
            }
        }






        //protected void btnModificar_Click(object sender, EventArgs e)
        //{
        //    FormView1.ChangeMode(FormViewMode.Edit);
        //    btnModificar.Visible = false;
        //    btnEliminar.Visible = false;

        //}

        //protected void btnEliminar_Click(object sender, EventArgs e)
        //{
        //    string id = Request.QueryString["id"];
        //    if (id != null)
        //    {
        //        SqlDataSource1.DeleteParameters["ID"].DefaultValue = id;

        //        SqlDataSource1.Delete();
        //        //SqlDataSource1.DataBind();
        //        Response.Redirect("contactos.aspx");
        //    }

        //}


    }
}